package com.travel.search.controller;

import com.travel.common.es.AttractionEsModel;
import com.travel.common.es.TicketEsModel;
import com.travel.common.exception.BizCodeEnum;
import com.travel.common.utils.R;
import com.travel.search.service.AttractionSaveService;
import com.travel.search.service.TicketSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;

import static com.travel.common.exception.BizCodeEnum.Attraction_UP_EXCEPTION;

@Slf4j
@RestController
@RequestMapping("/search/save")
public class ElasticSaveController {

    @Autowired
    AttractionSaveService attractionSaveService;

    @Autowired
    TicketSaveService ticketSaveService;

    /**
     * 保存景点信息到es中
     */
    @PostMapping("/attraction")
    public R attractionSave(@RequestBody AttractionEsModel attractionEsModel) {
        boolean b = false;
        try{
            b = attractionSaveService.attractionSave(attractionEsModel);
        }catch (Exception e){
            log.error("ElasticSaveController景点信息录入es错误:{}",e);
            return R.error(BizCodeEnum.Attraction_UP_EXCEPTION.getCode(), BizCodeEnum.Attraction_UP_EXCEPTION.getMessage());
        }
        if(!b){
            return R.ok();
        }else{
            return R.error(BizCodeEnum.Attraction_UP_EXCEPTION.getCode(), BizCodeEnum.Attraction_UP_EXCEPTION.getMessage());
        }
    }

    /**
     * 保存票务信息到es中
     */
    @PostMapping("/ticket")
    public R ticketSave(@RequestBody TicketEsModel ticketEsModel){
        boolean b =false;
        try{
            ticketSaveService.ticketSave(ticketEsModel);
        }catch (Exception e){
            log.error("ElasticSaveController票务信息录入es错误:{}",e);
            return R.error(BizCodeEnum.TICKET_UP_EXCEPTION.getCode(), BizCodeEnum.TICKET_UP_EXCEPTION.getMessage());
        }
        if(!b){
            return R.ok();
        }else{
            return R.error(BizCodeEnum.TICKET_UP_EXCEPTION.getCode(), BizCodeEnum.TICKET_UP_EXCEPTION.getMessage());
        }
    }

}
